﻿using System;
using System.Threading.Tasks;

using Windows.UI.Xaml;

using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

using Uhimania.Libraries.Test;

using Uhimania.Libraries.Windows.Interactivity;

namespace Windows.InteractivityTest
{
    [TestClass]
    public class BehaviorTest
    {
        #region Behavior

        #region 正常ケース
        [TestMethod]
        public async Task Behavior_正常ケース()
        {
            await UiTestHelper.ExecuteOnUiThread(() =>
            {
                var obj = new Mock();

                var mock = new BehaviorMock();

                mock.Attach(obj);
                mock.AssociatedObject.Is(obj);
                mock.attach.Is(true);

                mock.Detach();
                mock.AssociatedObject.IsNull();
                mock.detach.Is(true);
            });
        }
        #endregion

        #endregion

        #region Behavior<T>

        #region 正常ケース
        [TestMethod]
        public async Task Behavior_T_正常ケース()
        {
            await UiTestHelper.ExecuteOnUiThread(() =>
            {
                var obj = new Mock();

                var mock = new BehaviorTMock();

                mock.Attach(obj);
                mock.AssociatedObject.Is(obj);
                mock.attach.Is(true);

                mock.Detach();
                mock.AssociatedObject.IsNull();
                mock.detach.Is(true);
            });
        }
        #endregion

        #endregion

        #region Mock

        private class Mock : DependencyObject
        {
        }

        private class BehaviorMock : Behavior
        {
            public bool attach = false;
            protected override void OnAttached()
            {
                base.OnAttached();
                this.attach = true;
            }

            public bool detach = false;
            protected override void OnDetaching()
            {
                base.OnDetaching();
                this.detach = true;
            }
        }

        private class BehaviorTMock : Behavior<Mock>
        {
            public bool attach = false;
            protected override void OnAttached()
            {
                base.OnAttached();
                this.attach = true;
            }

            public bool detach = false;
            protected override void OnDetaching()
            {
                base.OnDetaching();
                this.detach = true;
            }
        }

        #endregion
    }
}
